<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <ul>
        <li>
          西游记：价格{{xyjPrice}}，数量：
          <input type="number" v-model="xyjNum" />
        </li>
        <li>
          水浒传：价格{{shzPrice}}，数量：
          <input type="number" v-model="shzNum" />
        </li>
        <li>总价：{{totalPrice}}</li>
        {{msg}}
      </ul>
    </div>

    <script src="../node_modules/vue/dist/vue.js"></script>

    <script>
      let app = new Vue({
        el: "#app",
        data: {
          xyjPrice: 56.73,
          shzPrice: 47.98,
          xyjNum: 1,
          shzNum: 1,
          msg: ""
        },
        computed: {
          totalPrice() {
            return this.xyjPrice * this.xyjNum + this.shzPrice * this.shzNum;
          },
        },
        watch: {
          xyjNum(newVal, oldVal) {
            if (newVal >= 3) {
              this.msg = "西游记没有更多库存了";
              this.xyjNum = 3;
            } else {
              this.msg = "";
            }
          },
        },
      });
    </script>
  </body>
</html>
